package main

import (
	"fmt"
	"os/exec"

	"github.com/ying32/govcl/vcl"
	"github.com/ying32/govcl/vcl/types"
)

// 创建生成文档页面
func (f *MainForm) createDocumentFrame(i int) {
	var (
		top       int32 = commonPadding
		dbIndex   int32 = -1
		modeIndex int32 = -1
	)

	// 创建容器
	f.contentFrame[i] = vcl.NewFrame(f)
	f.contentFrame[i].SetParent(f)
	f.contentFrame[i].SetAlign(types.AlClient)
	f.contentFrame[i].SetColor(contentColor)

	// 数据源列表
	dbListLabel := vcl.NewLabel(f)
	dbListLabel.SetParent(f.contentFrame[i])
	dbListLabel.SetCaption("选择数据源")
	dbListLabel.SetBounds(custom5LabelPadding, top+labelFix, formLabelWidth, formItemHeight)
	f.documentFrame.dbList = vcl.NewComboBox(f)
	f.documentFrame.dbList.SetParent(f.contentFrame[i])
	f.documentFrame.dbList.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)
	f.documentFrame.dbList.SetStyle(types.CsDropDownList)
	f.documentFrame.dbList.SetOnCloseUp(func(sender vcl.IObject) {
		f.fixComboBoxErr()
	})
	f.documentFrame.dbList.SetOnSelect(func(sender vcl.IObject) {
		dbIndex = f.documentFrame.dbList.ItemIndex()
		if documentConfigs != nil && dbIndex <= int32(len(documentConfigs)-1) {
			modeIndex = documentConfigs[dbIndex].Mode
			vcl.ThreadSync(func() {
				f.documentFrame.tables.SetText(documentConfigs[dbIndex].Tables)
				f.documentFrame.filePath.SetText(documentConfigs[dbIndex].FilePath)
				f.documentFrame.mode.SetItemIndex(documentConfigs[dbIndex].Mode)
			})
		} else {
			clear(f.documentFrame.tables, f.documentFrame.filePath, f.documentFrame.mode)
		}
	})
	for _, config := range dbConfigs {
		f.documentFrame.dbList.Items().Add(config.DBId)
	}
	renderDBList := vcl.NewButton(f)
	renderDBList.SetParent(f.contentFrame[i])
	renderDBList.SetCaption("刷新数据源")
	renderDBList.SetBounds(f.documentFrame.dbList.Left()+f.documentFrame.dbList.Width()+commonPadding, top, formButtonWidth, formItemHeight)
	renderDBList.SetOnClick(func(sender vcl.IObject) {
		loadConfigs(0)
		vcl.ThreadSync(func() {
			clear(f.documentFrame.tables, f.documentFrame.filePath, f.documentFrame.mode)
			f.documentFrame.dbList.Items().Clear()
			for _, dbConfig := range dbConfigs {
				f.documentFrame.dbList.Items().Add(dbConfig.DBId)
			}
			dbIndex = -1
		})
	})

	// 选择数据表
	top += commonPadding + formItemHeight
	f.documentFrame.tables = vcl.NewLabeledEdit(f)
	f.documentFrame.tables.SetParent(f.contentFrame[i])
	f.documentFrame.tables.EditLabel().SetCaption("选择数据表")
	f.documentFrame.tables.SetLabelPosition(types.LpLeft)
	f.documentFrame.tables.SetLabelSpacing(commonPadding)
	f.documentFrame.tables.SetReadOnly(true)
	f.documentFrame.tables.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)
	selectTables := vcl.NewButton(f)
	selectTables.SetParent(f.contentFrame[i])
	selectTables.SetBounds(f.documentFrame.tables.Left()+f.documentFrame.tables.Width()+commonPadding, top, formButtonWidth, formItemHeight)
	selectTables.SetCaption("选择表")
	selectTables.SetOnClick(func(sender vcl.IObject) {
		if dbIndex == -1 {
			vcl.ShowMessage("未选择数据源")
			return
		}
		f.selectTablesForm(dbIndex, func(tableStr string) {
			vcl.ThreadSync(func() {
				f.documentFrame.tables.SetText(tableStr)
			})
		})
	})

	// 生成位置
	top += commonPadding + formItemHeight
	f.documentFrame.filePath = vcl.NewLabeledEdit(f)
	f.documentFrame.filePath.SetParent(f.contentFrame[i])
	f.documentFrame.filePath.EditLabel().SetCaption("文件位置")
	f.documentFrame.filePath.SetLabelPosition(types.LpLeft)
	f.documentFrame.filePath.SetLabelSpacing(commonPadding)
	f.documentFrame.filePath.SetReadOnly(true)
	f.documentFrame.filePath.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)
	selectBasePath := vcl.NewButton(f)
	selectBasePath.SetParent(f.contentFrame[i])
	selectBasePath.SetCaption("选择文件夹")
	selectBasePath.SetBounds(f.documentFrame.filePath.Left()+f.documentFrame.filePath.Width()+commonPadding, top, formButtonWidth, formItemHeight)
	selectBasePath.SetOnClick(func(sender vcl.IObject) {
		if ok, dir := vcl.SelectDirectory1(0); ok {
			f.documentFrame.filePath.SetText(dir)
		}
	})

	// 选择生成模式
	top += commonPadding + formItemHeight
	modeLabel := vcl.NewLabel(f)
	modeLabel.SetParent(f.contentFrame[i])
	modeLabel.SetCaption("选择生成模式")
	modeLabel.SetBounds(custom6LabelPadding, top+labelFix, formLabelWidth, formItemHeight)
	f.documentFrame.mode = vcl.NewComboBox(f)
	f.documentFrame.mode.SetParent(f.contentFrame[i])
	f.documentFrame.mode.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)
	f.documentFrame.mode.SetStyle(types.CsDropDownList)
	f.documentFrame.mode.SetOnCloseUp(func(sender vcl.IObject) {
		f.fixComboBoxErr()
	})
	for i := 0; i < len(documentModes); i++ {
		f.documentFrame.mode.Items().Add(documentModes[i])
	}

	// 操作
	top += commonPadding*2 + formItemHeight
	execHandle := vcl.NewButton(f)
	execHandle.SetParent(f.contentFrame[i])
	execHandle.SetBounds(formLabelWidth+commonPadding, top, formButtonWidth, formItemHeight)
	execHandle.SetCaption("生成文档")
	execHandle.SetOnClick(func(sender vcl.IObject) {
		tablesText := f.documentFrame.tables.Text()
		filePathText := f.documentFrame.filePath.Text()
		if dbIndex == -1 || len(tablesText) == 0 || len(filePathText) == 0 || modeIndex == -1 {
			vcl.ShowMessage("数据填写不完整")
			return
		}
		exist := false
		if dbIndex <= int32(len(documentConfigs)-1) {
			for i := range documentConfigs {
				if documentConfigs[i].DBId == documentConfigs[dbIndex].DBId {
					exist = true
					documentConfigs[i].Tables = tablesText
					documentConfigs[i].FilePath = filePathText
					documentConfigs[i].Mode = modeIndex
					break
				}
			}
		}
		if !exist {
			documentConfig := DocumentConfig{
				DBId:     dbConfigs[dbIndex].DBId,
				Tables:   tablesText,
				FilePath: filePathText,
				Mode:     modeIndex,
			}
			documentConfigs = append(documentConfigs, documentConfig)
		}
		f.consoleForm.Show()
		go func() {
			if modeIndex == 0 {
				f.runExcel(dbIndex, f.documentFrame.tables.Text(), f.documentFrame.filePath.Text(), func(log string) {
					vcl.ThreadSync(func() {
						f.consoleLog.Lines().Add(log)
					})
				})
			} else if modeIndex == 1 {
				fmt.Println(modeIndex)
			}
			reloadConfigs(2)
		}()
	})

	openDirHandle := vcl.NewButton(f)
	openDirHandle.SetParent(f.contentFrame[i])
	openDirHandle.SetBounds(execHandle.Left()+execHandle.Width()+commonPadding, top, formButtonWidth, formItemHeight)
	openDirHandle.SetCaption("打开目录")
	openDirHandle.SetOnClick(func(sender vcl.IObject) {
		if len(f.documentFrame.filePath.Text()) == 0 {
			vcl.ShowMessage("未选择目录")
			return
		}
		_ = exec.Command(`cmd`, `/c`, `explorer`, f.documentFrame.filePath.Text()).Start()
	})

	f.documentFrame.mode.SetOnSelect(func(sender vcl.IObject) {
		top = f.documentFrame.mode.Top()
		modeIndex = f.documentFrame.mode.ItemIndex()
	})
}
